package com.iot.commonutils.utils;

import lombok.Data;

/**
 * 统一的返回结果类
 *
 * @author 孟繁森
 * @date 2021/3/12 17:46
 */

@Data
public final class Result<T> {
    private Integer code;//状态码

    private String status;//状态码对应的信息

    private String message;//返回的提示信息

    private T data; //返回的数据

    public static <T> Result<T> success(String message) {
        return new Result<>(ResultCode.SUCCESS, "成功", message);
    }

    public static <T> Result<T> success(String message, T data) {
        return new Result<>(ResultCode.SUCCESS, "成功", message, data);
    }

    public static <T> Result<T> success(Integer code, String message, T data) {
        return new Result<>(code, "成功", message, data);
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(ResultCode.FAIL, "失败", message);
    }

    public static <T> Result<T> error(String message, T data) {
        return new Result<>(ResultCode.FAIL, "失败", message, data);
    }

    public static <T> Result<T> error(Integer code, String message, T data) {
        return new Result<>(code, "失败", message, data);
    }

    public static Integer getSuccessCode() {
        return ResultCode.SUCCESS;
    }

    public static Integer getErrorCode() {
        return ResultCode.FAIL;
    }


    private Result(Integer code, String status, String message, T data) {
        this.code = code;
        this.status = status;
        this.message = message;
        this.data = data;
    }

    private Result(Integer code, String status, String message) {
        this.code = code;
        this.status = status;
        this.message = message;
    }

    private Result() {
    }

    @Override
    public String toString() {
        return "Result [code=" + code + ", status=" + status + ", message=" + message
                + ", data=" + data + "]";
    }

}
